package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemContactUsBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemContactUsDao;
import com.cw.jeeyt.dao.model.system.SystemContactUsDo;
import com.cw.jeeyt.service.converter.system.SystemContactUsConverter;
import com.cw.jeeyt.service.intf.system.SystemContactUsService;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 联系我们业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemContactUsServiceImpl implements SystemContactUsService {

    private static final Logger logger = LoggerFactory.getLogger(SystemContactUsServiceImpl.class);
    @Autowired
    private SystemContactUsDao systemContactUsDao;//联系我们数据库操作接口

    /**
     * 查询所有联系我们
     */
    @Override
    public List<SystemContactUsBo> querySystemContactUsList() throws Exception {
        return SystemContactUsConverter.doList2BoList(systemContactUsDao.querySystemContactUsList());
    }

    /**
     * 分页查询联系我们
     *
     * @param systemContactUsBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemContactUsBo> queryByPage(SystemContactUsBo systemContactUsBo, Integer startNum,
        Integer pageSize) throws Exception {
        PageInfo<SystemContactUsDo> pageInfo = systemContactUsDao.queryByPage(
            SystemContactUsConverter.bo2Do(systemContactUsBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemContactUsConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻联系我们信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemContactUsBo getSystemContactUsById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }

        return SystemContactUsConverter.do2Bo(systemContactUsDao.getById(id));
    }

    /**
     * 新增联系我们
     *
     * @param systemContactUsBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SystemContactUsBo systemContactUsBo) throws Exception {

        if (systemContactUsBo == null) {
            logger.error("联系我们为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemContactUsDo systemContactUs = SystemContactUsConverter.bo2Do(systemContactUsBo);
        systemContactUs.setCreateBy(userVo.getId());
        systemContactUs.setCreateDate(new Date());
        systemContactUs.setUpdateBy(userVo.getId());
        systemContactUs.setUpdateDate(new Date());
        Integer result = systemContactUsDao.insert(systemContactUs);
        return result;
    }

    /**
     * 更新联系我们信息
     *
     * @param systemContactUsBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SystemContactUsBo systemContactUsBo) throws Exception {

        if (systemContactUsBo == null) {
            logger.error("联系我们为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemContactUsDo systemContactUs = SystemContactUsConverter.bo2Do(systemContactUsBo);
        systemContactUs.setUpdateBy(userVo.getId());
        systemContactUs.setUpdateDate(new Date());

        systemContactUsDao.update(systemContactUs);
    }

    /**
     * 删除联系我们
     *
     * @param systemContactUsBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SystemContactUsBo systemContactUsBo) throws Exception {

        if (systemContactUsBo == null) {
            logger.error("联系我们为空");
            return;
        }
        systemContactUsDao.delete(SystemContactUsConverter.bo2Do(systemContactUsBo));
    }
}
